#include "window_factory.h"

#include <sstream>


WindowFactory &WindowFactory::instance()
{
    static WindowFactory sInstance;
    return sInstance;
}

void WindowFactory::addCreator(const std::string &name, std::function<GlWindow *()> &&creator)
{
    creators_[name] = std::move(creator);
}

std::unique_ptr<GlWindow> WindowFactory::create(const std::string &name) const
{
    auto it = creators_.find(name);
    if (it == creators_.end()) {
        std::ostringstream ss;
        ss << "Cannot find creator by name [" << name << "]";
        throw std::runtime_error(name);
    }

    return std::unique_ptr<GlWindow>(it->second());
}

WindowFactory::WindowFactory()
{

}

WindowFactory::~WindowFactory()
{

}

WindowFactory::Register::Register(const std::string &name, std::function<GlWindow *()> &&creator)
{
    WindowFactory::instance().addCreator(name, std::move(creator));
}
